package com.demo.monitor;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.Tailer;
import org.apache.commons.io.input.TailerListenerAdapter;

import java.io.File;
import java.io.IOException;
import java.util.Date;

public class MonitorTest2 {

    public static void main(String[] args) throws  Exception {
        File file = new File("./1.txt");

        if(!file.exists()) {
            System.out.println("log file is not existing.");
            return;
        }
        TailerListenerAdapter listener = new TailerListenerAdapter() {
            Tailer tailer;
            int count;

            @Override
            public void init(Tailer tailer) {
                this.tailer = tailer;
            }

            @Override
            public void handle(String line) {
                if(++ count % 200 == 0) {
                    System.out.println("stop to tail....");
                    this.tailer.stop();
                }
                System.out.println("2:" + line);
                super.handle(line);
            }

            @Override
            public void handle(Exception ex) {
                ex.printStackTrace();
                super.handle(ex);
            }

            @Override
            public void fileRotated() {
                System.out.println("文件rotated");
                super.fileRotated();
            }

            @Override
            public void fileNotFound() {
                System.out.println("文件没有找到");
                super.fileNotFound();
            }

            @Override
            public void endOfFileReached() {
                System.out.println("ending.....");
            }
        };

        Tailer tailer = new Tailer(file, listener, 3000, true);
        new Thread(tailer, "myTailer2").start();

    }
}
